package com.seirsoft.framework.payload;

import java.lang.reflect.Type;
import com.seirsoft.framework.util.Common;

public class PayloadColumn
{
    // Fields
    private Type m_dataType;
    private boolean m_isValue;
    private String m_name;
    private int m_offset;
    
    private boolean m_isVisible;
    
    public boolean isVisible()
    {
    	return this.m_isVisible;
    }
    
    private PayloadColumn()
    {
    	this.setDataType(String.class);

    	this.m_name = "";
        this.m_offset = 0;
        
        this.m_isVisible = true;
    }
    
    public PayloadColumn(
    		Type type,
    		String name,
    		boolean isVisible)
    {
    	this.setDataType(type);
    	
    	this.m_name = name;
        this.m_offset = 0;
        
        this.m_isVisible = isVisible;
    }
    
    private void setDataType(Type type)
    {
    	this.m_dataType = type;
    	
    	this.m_isValue = type.equals(int.class)
        		|| type.equals(boolean.class);
    }
    
    public Type getDataType()
    {
    	return this.m_dataType;
    }
    
    public boolean getIsValue()
    {   		
    	return this.m_isValue;
    }
    
    public void setOffset(int i)
    {
    	this.m_offset = i;
    }
    
    public int getOffset()
    {
    	return this.m_offset;
    }
    
	public String getName()
	{
		return this.m_name;
	}
	
    /**
     *  Factory class that keeps the parse method
     */
    public static class Factory {
        /**
         * static method to create the object
         * Precondition:  If this object is an element, the current or next start element starts this object and any intervening reader events are ignorable
         *                If this object is not an element, it is a complex type and the reader is at the event just after the outer start element
         * Postcondition: If this object is an element, the reader is positioned at its end element
         *                If this object is a complex type, the reader is positioned at the end element of its outer element
         */
        public static PayloadColumn parse(
            javax.xml.stream.XMLStreamReader reader) throws java.lang.Exception {
        	
        	PayloadColumn object = new PayloadColumn();

            try {

                while (!reader.isStartElement() && !reader.isEndElement())
                    reader.next();

            	//<n>
                do{
                	reader.next();
                } while (!reader.isStartElement() && !reader.isEndElement());
                
                if (reader.isStartElement()
                		&& reader.getLocalName().equals("n"))
                {               	
                	object.m_name = reader.getElementText();
                }
                
                //<t>
                do{
                	reader.next();
                } while (!reader.isStartElement() && !reader.isEndElement());
                
                if (reader.isStartElement()
                		&& reader.getLocalName().equals("t"))
                {
                	String strType = reader.getElementText();
                	object.setDataType(Common.getJavaType(strType));
                }
                
                // </c>
                do{
                	reader.next();
                } while (!reader.isStartElement() && !reader.isEndElement());

            } catch (javax.xml.stream.XMLStreamException e) {
                throw new java.lang.Exception(e);
            }

            return object;
        }
    } //end of factory class
}
